package com.service.scheduling.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.service.dao.enums.CommonStatusEnums;
import com.service.dao.enums.GradeEnum;
import com.service.dao.enums.StarGradeEnum;
import com.service.dao.mapper.MemberMapper;
import com.service.dao.model.Member;
import com.service.dao.po.MemberCache;
import com.service.scheduling.service.MemberCacheService;
import com.service.scheduling.service.MemberService;
import com.service.scheduling.service.MemberStarGradeConfigService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <dl>
 * <dt>back-end</dt>
 * <dd>Description:</dd>
 * <dd>Copyright: Copyright (C) 2015</dd>
 * <dd>Company: </dd>
 * <dd>CreateDate: 2019年09月19日</dd>
 * </dl>
 *
 * @author
 */
@Slf4j
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {

    private static String path = "-{memberId}-";

    @Autowired
    private MemberCacheService memberCacheService;

    @Autowired
    private MemberStarGradeConfigService starGradeConfigService;

    @Override
    public List<Member> listAll() {
        return list(new QueryWrapper<Member>().lambda()
                .eq(Member::getUserStatus, CommonStatusEnums.ACTIVE.getCode())
                .eq(Member::getActivate, CommonStatusEnums.ACTIVE.getCode())
                .orderByDesc(Member::getVipGrade));
    }

    @Override
    public List<Integer> listAllMemberIdsByStatus(Integer userStatus) {
        return baseMapper.listAllMemberIdsByStatus(userStatus);
    }

    @Override
    public List<Member> listStarMember() {
        return list(new QueryWrapper<Member>().lambda()
                .in(Member::getStarGrade, StarGradeEnum.getStarGrade())
                .eq(Member::getUserStatus, CommonStatusEnums.ACTIVE.getCode())
                .eq(Member::getActivate, CommonStatusEnums.ACTIVE.getCode())
                .orderByDesc(Member::getStarGrade));
    }

    @Override
    public List<Member> listMaxGradeMember() {

        Integer maxVipGrade = baseMapper.getMaxVipGrade(CommonStatusEnums.ACTIVE.getCode(),
                CommonStatusEnums.ACTIVE.getCode());

        return list(new QueryWrapper<Member>().lambda()
                .eq(Member::getVipGrade, maxVipGrade)
                .eq(Member::getUserStatus, CommonStatusEnums.ACTIVE.getCode())
                .eq(Member::getActivate, CommonStatusEnums.ACTIVE.getCode()));

    }

    @Override
    public List<Member> listByPid(Integer pId) {
        return list(new QueryWrapper<Member>().lambda().eq(Member::getPid, pId)
                .eq(Member::getUserStatus, CommonStatusEnums.ACTIVE.getCode())
                .eq(Member::getActivate, CommonStatusEnums.ACTIVE.getCode()));
    }

    @Override
    public List<Member> listByPathLike(Integer memberId) {
        return list(new QueryWrapper<Member>().lambda()
                .like(Member::getUserPath, path.replace("{memberId}", memberId.toString()))
                .eq(Member::getUserStatus, CommonStatusEnums.ACTIVE.getCode())
                .eq(Member::getActivate, CommonStatusEnums.ACTIVE.getCode()));
    }

    @Override
    public List<Integer> getMemberPathList(String memberPath) {
        return Lists.newArrayList(memberPath.split("-"))
                .stream().filter(StringUtils::isNotBlank).map(Integer::parseInt).collect(Collectors.toList());
    }

    @Override
    public List<Member> getMemberByPath(String memberPath){
        List<Integer> memberId = getMemberPathList(memberPath);
        if(ObjectUtils.isEmpty(memberId)){
            return Lists.newArrayList();
        }
        return list(new QueryWrapper<Member>().lambda()
                .in(Member::getId, memberId)
                .eq(Member::getUserStatus, CommonStatusEnums.ACTIVE.getCode())
                .eq(Member::getActivate, CommonStatusEnums.ACTIVE.getCode()));
    }

    @Override
    public List<Member> listByVipGrade(Integer vipGrade) {
        return list(new QueryWrapper<Member>().lambda()
                .eq(Member::getVipGrade, vipGrade)
                .eq(Member::getUserStatus, CommonStatusEnums.ACTIVE.getCode())
                .eq(Member::getActivate, CommonStatusEnums.ACTIVE.getCode()));
    }

    @Override
    public List<Member> listByGradeList(List<Integer> vipGradeList) {
        return list(new QueryWrapper<Member>().lambda()
                .in(Member::getVipGrade, vipGradeList)
                .eq(Member::getUserStatus, CommonStatusEnums.ACTIVE.getCode())
                .eq(Member::getActivate, CommonStatusEnums.ACTIVE.getCode()));
    }

    @Override
    public List<Member> listByPathLikeAndVipGrade(Integer memberId, Integer vipGrade) {
        return list(new QueryWrapper<Member>().lambda()
                .like(Member::getUserPath, path.replace("{memberId}", memberId.toString()))
                .eq(Member::getVipGrade, vipGrade)
                .eq(Member::getUserStatus, CommonStatusEnums.ACTIVE.getCode())
                .eq(Member::getActivate, CommonStatusEnums.ACTIVE.getCode()));
    }

    @Override
    public List<Member> selectByPathAndStatus(Integer memberId) {
        return list(new QueryWrapper<Member>().lambda()
                .like(Member::getUserPath, "-" + memberId + "-")
                .eq(Member::getUserStatus, CommonStatusEnums.ACTIVE.getCode())
                .eq(Member::getActivate, CommonStatusEnums.ACTIVE.getCode()));
    }

    @Override
    public Integer countMemberByPidAndStarGrade(Integer memberId, List<Integer> grade) {
        return baseMapper.countByParentIdAndStarGrade(memberId, grade);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void checkAndUpdateMemberStarGrade(MemberCache memberCache) {
        Integer starGrade = starGradeConfigService.checkMemberStarGrade(memberCache);
        if (!ObjectUtils.nullSafeEquals(starGrade, memberCache.getStarGrade())) {
            memberCache.setStarGrade(starGrade);
            updateMemberStarGrade(memberCache.getMemberId(), starGrade);
            memberCacheService.setMemberCache(memberCache);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateMemberStarGrade(Integer memberId, Integer grade) {
        baseMapper.updateById(Member.builder().id(memberId).starGrade(grade).build());
    }

    /**
     * @param gradeList
     * @Description: 获取用户等级处理列表中的用户集合
     * @Author: ty
     * @Date: 2021/5/6 16:12
     * @param: [grades]
     * @return: java.util.List<java.lang.Integer>
     */
    @Override
    public List<Member> getMemberIdsByGradeList(List<Integer> gradeList) {
        return list(new QueryWrapper<Member>().lambda()
                .in(Member::getVipGrade, gradeList)
        );
    }
}
